package com.vm.db;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 数据库帮助类
 * 
 * @author WikerYong Email:<a href="#">yw_312@foxmail.com</a>
 * @version 2012-3-31 上午11:16:03
 */
public class DBOpenHelper extends SQLiteOpenHelper {

	private static final String TAG = "DBOpenHelper";

	public static SQLiteDatabase db = null;

	// private boolean isUpgrade = false;

	public DBOpenHelper(Context context) {
		super(context, "download.db", null, 12);
		db = getWritableDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		if (DBOpenHelper.db == null) {
			DBOpenHelper.db = db;
		}
		try {
			if (!tabbleIsExist("downInfo")) {
				db.execSQL("CREATE TABLE downInfo (path VARCHAR(1024), softId VARCHAR(50),name varchar(50),imageUrl varchar(200),isPause INTEGER, done INTEGER,apkLength INTEGER,completed Integer,apkUrl varchar(200), PRIMARY KEY(path, softId))");
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
		}

		try {
			if (!tabbleIsExist("upgradeSoft")) {
				db.execSQL("CREATE TABLE upgradeSoft (id VARCHAR(40), softId VARCHAR(50),name varchar(50),packageName varchar(100),currentVersion varchar(20),newVersion varchar(20), size float, PRIMARY KEY(id))");
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
		}

		try {
			if (!tabbleIsExist("collection")) {
				db.execSQL("CREATE TABLE collection (softId VARCHAR(50),name varchar(50),imageUrl varchar(200),size float,star int,apkUrl varchar(200), PRIMARY KEY(softId))");
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
		}

		try {
			if (!tabbleIsExist("operation")) {
				db.execSQL("CREATE TABLE operation (softId VARCHAR(50),packageName varchar(100), PRIMARY KEY(softId))");
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
		}

		try {
			if (!tabbleIsExist("goodscollection")) {
				db.execSQL("CREATE TABLE goodscollection (goodsId VARCHAR(50),name varchar(50),imageUrl varchar(200),price float,PRIMARY KEY(goodsId))");
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
		}

		try {
			// 存在则删除,当此数据表更新迭代后,无变化,则不删除
			if (tabbleIsExist("LocalUserInfo")) {
				db.execSQL("drop TABLE LocalUserInfo");
			}
			db.execSQL("CREATE TABLE LocalUserInfo(userId varchar(40),username varchar(40),intergral INTEGER,imageUrl varchar(200),psd varchar(20),nickname varchar(20),phone varchar(20),invCode varchar(10),PRIMARY KEY(userId))");

		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
		}

		try {
			if (!tabbleIsExist("rememberPsd")) {
				db.execSQL("CREATE TABLE rememberPsd(userId varchar(40),username varchar(40),psd varchar(20),PRIMARY KEY(userId))");
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
		}

		try {
			if (!tabbleIsExist("setting")) {
				db.execSQL("CREATE TABLE setting(nopic INTEGER,onlywifi INTEGER,zeroflow INTEGER,autoinstall INTEGER,autodelete INTEGER,quickinstall INTEGER,updatenotification INTEGER,autorotation INTEGER,PRIMARY KEY(nopic))");
				db.execSQL("INSERT INTO setting(nopic,onlywifi,zeroflow,autoinstall,autodelete,quickinstall,updatenotification,autorotation)VALUES(0,0,0,1,0,0,1,0)");
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
		}

		try {
			if (!tabbleIsExist("shoppingcart")) {
				db.execSQL("CREATE TABLE shoppingcart(goodsId varchar(40),name varchar(40),imageUrl varchar(200),price float,number INTEGER,PRIMARY KEY(goodsId))");
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onCreate(db);
	}

	/**
	 * 判断某张表是否存在
	 * 
	 * @param tabName
	 *            表名
	 * @return
	 */
	public boolean tabbleIsExist(String tableName) {
		boolean result = false;
		if (tableName == null) {
			return false;
		}
		Cursor cursor = null;
		try {
			String sql = "select count(*) as c from Sqlite_master  where type ='table' and name ='"
					+ tableName.trim() + "' ";
			cursor = db.rawQuery(sql, null);
			if (cursor.moveToNext()) {
				int count = cursor.getInt(0);
				if (count > 0) {
					result = true;
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (null != cursor && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return result;
	}

}